import Vue from 'vue';
import Router from 'vue-router';
import { routes } from './routes';
import { Auth } from '@/providers';
import { store } from '@/store';
Vue.use(Router);
const vueRouter = new Router({
  mode: 'history',
  routes
});
// 路由导航守卫
vueRouter.beforeEach((to, from, next) => {
  // 存储登陆状态
  store.dispatch('LOGINSTATUS', Auth.checkItem('token'));
  // 判断是否是第一次进入页面且已登陆，获取用户绑定状态
  if (!store.getters.fistLoad) {
    store.dispatch('GETALLCONTACT');
    store.dispatch('FIRSTLOAD', true);
    if (to.matched[0].meta.index) {
      store.dispatch('SIDEBARINDEX', { index: to.matched[0].meta.index, type: 'index' });
    }
    if (Auth.checkItem('token')) {
      store.dispatch('GETUSERTATUS', next);
    }
  }
  if (to.meta.token && Auth.checkItem('token')) {
    next(from.path);
  }
  next();
});
export {
  vueRouter
};